import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index/test_case'
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import(/* webpackChunkName: "about" */ '../views/Index.vue'),
    children: [
      {
        path: 'test_case',
        name: 'TestCase',
        component: () => import(/* webpackChunkName: "about" */ '../views/TestCase.vue'),
      },
      {
        path: 'test_plan',
        name: 'TestPlan',
        component: () => import(/* webpackChunkName: "about" */ '../views/TestPlan.vue'),
      },
      {
        path: 'test_report',
        name: 'TestReport',
        component: () => import(/* webpackChunkName: "about" */ '../views/TestReport.vue'),
      },
    ]
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),
  },
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 1. 判断是否要去登录页面
  if (to.path === '/login') {
    next();
  } else {
    // 2.如果去其他页面，验证是否登录过
    const token = localStorage.getItem('token')
    console.log('token====', token)
    // 3. 如果没有登录过
    if (!token) {
      // 4. 强制进入登录页面
      next('/login');
    } else {
      next();
    }
  }
})

export default router
